package com.tc.gateway.event;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class EventCenter {
	static EventCenter instance;
	static{
		instance = new EventCenter();
	}
	static public EventCenter getInstance(){
		return instance;
	}
	Map< Class<? extends Event>, List<EventListener>> listeners = new HashMap< Class<? extends Event>, List<EventListener>>();
	
	public void fireEvent(Event e){
		List<EventListener> ls = listeners.get(e.getClass());
		for(EventListener l : ls)
			l.onEvent(e);
	}
	public void register(EventListener l, Class<? extends Event> type){
		List<EventListener> ls = listeners.get(type);
		if(ls == null){
			ls = new LinkedList<EventListener>();
			listeners.put(type, ls);
		}
		if(ls.contains(ls))
			return;
		ls.add(l);
	}
	public void remove(EventListener l, Class<? extends Event> type){
		List<EventListener> ls = listeners.get(type);
		ls.remove(l);
		
	}
}
